import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/products_list_vo.dart';

class ActivityGoodPageController extends GetxController {

  static List<Map<String, String>> sortArr = [
    {
      'type': 'id',
      'value': 'asc'
    },
    {
      'type': 'sales',
      'value': 'asc'
    },
    {
      'type': 'price',
      'value': 'asc'
    },
    {
      'type': 'price',
      'value': 'desc'
    }
  ];


  String keyword = '';
  // int page = 1;
  // int categoryId = 0;
  /// 排序下标 0 综合， 1 销量， 2 价格高到低， 3 价格低到高
  var sortIndex = 0.obs;
  /// 价格正序
  var priceSortDesc = true.obs;

  /// 商品对象
  var productsListVo = ProductsListVo(data: [], meta: Meta(pagination: CommonPagination(
    currentPage: 1,
    total: 0
  ))).obs;

  /// 活动ID
  int activityId = 0;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    activityId = argumentsMap['activityId'];
    getActivityGoodList();
    super.onReady();
  }
  
  /// 获取活动商品列表
  getActivityGoodList() async {
    productsListVo.value.meta!.pagination!.currentPage = 1;
    String orderType = sortArr[sortIndex.value]['type'] as String;
    String sort = sortArr[sortIndex.value]['value'] as String;
    ProductsListVo tempVo = await HttpUtilService.getActivityGoodList(keyword: keyword, activityId: activityId, orderType: orderType, sort: sort, page: productsListVo.value.meta!.pagination!.currentPage!);
    productsListVo.value = tempVo;
    update();
  }
  
  /// 加载活动更多商品
  onloadActivityGoodList() async {
    if (productsListVo.value.meta!.pagination!.total! <= productsListVo.value.data!.length) {
      return;
    }
    productsListVo.value.meta!.pagination!.currentPage = productsListVo.value.meta!.pagination!.currentPage! + 1;
    String orderType = sortArr[sortIndex.value]['type'] as String;
    String sort = sortArr[sortIndex.value]['value'] as String;
    ProductsListVo tempVo = await HttpUtilService.getActivityGoodList(keyword: keyword, activityId: activityId, orderType: orderType, sort: sort, page: productsListVo.value.meta!.pagination!.currentPage!);
    productsListVo.value.data!.addAll(tempVo.data!);
    update();
  }

}